//
//  MessageCenterCell.m
//  JinMaJiaDemo
//
//  Created by GripMeMory on 16/1/19.
//  Copyright © 2016年 GripMeMory. All rights reserved.
//

#import "MessageCenterCell.h"

#define kSeparateMargin 20 / 2
#define kImageViewLeftMargin 55 / 2
#define kImageViewWidth 60 / 2
#define kImageViewRightMargin 25 / 2
#define kTitleLabelTopMargin 20 / 2
#define kTitleSeparateMargin 10 / 2
#define kTitleLabelRightMargin 35 / 2
#define kTitleLabelHeight 30 / 2
#define kMessageLabelHeight 25 / 2
#define kTimeLabelHeight 25 / 2
#define kTimeLabelWidth 250 / 2
#define kTimeViewRightMargin 10 / 2

@implementation MessageCenterCell

{
    UIView *_backGroundView;
    UIImageView *_imageView;
    UILabel *_titleLabel;
    UILabel *_messageLabel;
    UILabel *_timeLabel;
    UIImageView *_timeView;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _backGroundView = [[UIView alloc] init];
        _backGroundView.backgroundColor = [UIColor whiteColor];
        
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.backgroundColor = [UIColor whiteColor];
        _titleLabel.adjustsFontSizeToFitWidth = YES;
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.font = [UIFont systemFontOfSize:14];
        
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.textColor = RGB_COLOR(@"#c9c9c9");
        _timeLabel.font = [UIFont systemFontOfSize:12];
        _timeLabel.textAlignment = NSTextAlignmentRight;
        
        _timeView = [[UIImageView alloc] init];
        _timeView.image = [UIImage imageNamed:@"时钟"];
        _timeView.contentMode = UIViewContentModeScaleAspectFit;
        
        _imageView = [[UIImageView alloc] init];
        
        _messageLabel = [[UILabel alloc] init];
        _messageLabel.font = [UIFont systemFontOfSize:12];
        
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor clearColor];
        [_backGroundView addSubview:_titleLabel];
        [_backGroundView addSubview:_timeLabel];
        [_backGroundView addSubview:_imageView];
        [_backGroundView addSubview:_messageLabel];
        [_backGroundView addSubview:_timeView];
        [self addSubview:_backGroundView];
    }
    return self;
}

- (void)layoutSubviews{
    _backGroundView.frame = CGRectMake(0, kSeparateMargin, self.width, self.height - kSeparateMargin);
    _imageView.frame = CGRectMake(kImageViewLeftMargin, 0, kImageViewWidth, self.height);
    _titleLabel.frame = CGRectMake(_imageView.right + kImageViewRightMargin, kTitleLabelTopMargin, self.width - kImageViewLeftMargin - kImageViewRightMargin - kTitleLabelRightMargin - kImageViewWidth, kTitleLabelHeight);
    _messageLabel.frame = CGRectMake(_titleLabel.left, _titleLabel.bottom + kTitleSeparateMargin, _titleLabel.width, kMessageLabelHeight);
    _timeLabel.frame = CGRectMake(self.right - kTitleLabelRightMargin - kTimeLabelWidth , _messageLabel.bottom + kTitleSeparateMargin, kTimeLabelWidth, kTimeLabelHeight);
    CGRect rect = _timeLabel.frame;
    CGRect rect2 = [_timeLabel textRectForBounds:rect limitedToNumberOfLines:1];
    _timeLabel.frame = CGRectMake(self.right - kTitleLabelRightMargin - rect2.size.width, rect.origin.y, rect2.size.width, rect.size.height);
    _timeView.frame = CGRectMake(_timeLabel.left - kTimeViewRightMargin - kTimeLabelHeight , _timeLabel.top, kTimeLabelHeight, kTimeLabelHeight);
}

- (void)configureCellWithModel:(MessageModel *)model{
    _imageView.image = [UIImage imageNamed:@"密码"];
    _imageView.contentMode = UIViewContentModeCenter;
    _titleLabel.text = model.title;
    _messageLabel.text = model.mainMessage;
    _timeLabel.text = model.time;
    CGRect rect = _timeLabel.frame;
    CGRect rect2 = [_timeLabel textRectForBounds:rect limitedToNumberOfLines:1];
    _timeLabel.frame = CGRectMake(self.right - kTitleLabelRightMargin - rect2.size.width, rect.origin.y, rect2.size.width, rect.size.height);
}

@end
